﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using ESF.General;
using System.Web.UI.WebControls;

#endregion


namespace ESF.Web
{
	public static class MenuLoader
	{
		#region Carga de Menu

		public static void Clear (Menu pMenu)
		{
			pMenu.Items.Clear ();
			pMenu.DataSource = null;
			pMenu.Visible = false;
		}
		

		public static bool Load (Menu pMenu, DataSet pdtsSet)
		{
			return Load (pMenu, DataHelper.GetFirstDataTable (pdtsSet));
		}


		public static bool Load (Menu pMenu, DataTable pTable)
		{
			if (DataHelper.IsEmpty (pTable)) { Clear (pMenu); return false; }

			return Load (pMenu, pTable,
				pTable.Columns [0].ColumnName, pTable.Columns [1].ColumnName, pTable.Columns [2].ColumnName,
				pTable.Columns [3].ColumnName, pTable.Columns [4].ColumnName, false);
		}


		public static bool Load (Menu pMenu, DataTable pTable,
			string pMenuID, string pNameField, string pDescriptionField,
			string pParentMenuID, string pURLField, bool pIncludeParent)
		{
			DataRow [] dtrNodosPrincipales;

			MenuItem objMenuItem;

			int intI;


			Clear (pMenu);

			if (DataHelper.IsEmpty (pTable)) { return false; }

			// Se buscan las hojas principales
			dtrNodosPrincipales = pTable.Select (string.Format ("{0} IS NULL", pParentMenuID));

			if (dtrNodosPrincipales.Length == 0) { return false; }

			foreach (DataRow dtrNivel in dtrNodosPrincipales)
			{
				objMenuItem = new MenuItem (dtrNivel [pNameField].ToString (), dtrNivel [pMenuID].ToString (), string.Empty, dtrNivel [pURLField].ToString (), string.Empty);
                if(!(dtrNivel[pDescriptionField] is DBNull)) {
                    objMenuItem.ToolTip = dtrNivel[pDescriptionField].ToString();
                }
                else {
                    objMenuItem.ToolTip = dtrNivel[pNameField].ToString();
                }
				CargarNivelMenu (objMenuItem, ref pTable, pMenuID, pNameField, pDescriptionField, pParentMenuID, pURLField);

				if (pIncludeParent)
				{
					pMenu.Items.Add (objMenuItem);
				}
				else
				{
					intI = objMenuItem.ChildItems.Count;
					while (intI > 0)
					{
						pMenu.Items.Add (objMenuItem.ChildItems [0]);
						intI--;
					}
				}
			}

			pMenu.Visible = true;

			return true;
		}


		private static void CargarNivelMenu (MenuItem pMenuItem, ref DataTable pTable,
			string pMenuID, string pNameField, string pDescriptionField,
			string pParentMenuID, string pURLField)
		{
			DataRow [] dtrNodosHijo;

			MenuItem objMenuItem;


			dtrNodosHijo = pTable.Select (string.Format ("{0} = {1}", pParentMenuID, pMenuItem.Value));

			foreach (DataRow dtrNivel in dtrNodosHijo)
			{
				objMenuItem = new MenuItem (dtrNivel [pNameField].ToString (), dtrNivel [pMenuID].ToString (), string.Empty, dtrNivel [pURLField].ToString (), string.Empty);
                if(!(dtrNivel[pDescriptionField] is DBNull)) {
                    objMenuItem.ToolTip = dtrNivel[pDescriptionField].ToString();
                }
                else {
                    objMenuItem.ToolTip = dtrNivel[pNameField].ToString();
                }
				CargarNivelMenu (objMenuItem, ref pTable, pMenuID, pNameField, pDescriptionField, pParentMenuID, pURLField);

				pMenuItem.ChildItems.Add (objMenuItem);
			}
		}

		#endregion
		
	}
}
